package org.colafries.admin.querybuilder.entity;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.MapUtils;
import org.colafries.admin.entity.domain.index.LogicCondition;
import org.colafries.admin.entity.domain.index.LogicFactTable;
import org.colafries.admin.entity.domain.index.LogicFactTableField;
import org.colafries.admin.entity.vo.index.DerivativeIndexDTO;
import org.colafries.admin.entity.vo.index.LogicDimensionDTO;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ann
 * @version v1.0
 * @ClassName IndexQueryParamContext
 * @Description
 * @date Created in 2024/6/19
 **/
@Data
public class IndexQueryParamContext {
    @ApiModelProperty(value = "派生指标DTOList")
    private List<DerivativeIndexDTO> derivativeIndexDTOList = Lists.newArrayList();

    @ApiModelProperty(value = "业务限定Map(key:业务限定code)")
    private Map<Long, LogicCondition> conditionMap = Maps.newHashMap();

    @ApiModelProperty(value = "事实逻辑表字段codeList")
    private List<Long> factTableFieldCodeList = Lists.newArrayList();
    /**
     * 根据事实逻辑表字段codeList生成
     */
    @ApiModelProperty(value = "事实逻辑表字段Map(key:逻辑表字段code)")
    private Map<Long, LogicFactTableField> factTableFieldMap = Maps.newHashMap();

    @ApiModelProperty(value = "逻辑维度List")
    private List<LogicDimensionDTO> dimensionDTOList = Lists.newArrayList();

    @ApiModelProperty(value = "派生指标使用的逻辑表")
    private LogicFactTable factTable;

    /**
     * 获取去重的fieldCodeList
     *
     * @return java.util.List<java.lang.Long>
     * @Author anning
     * @Date 2024/7/5
     */
    public List<Long> getUniqueFactTableFieldCodeList() {
        return factTableFieldCodeList.stream()
                .distinct().collect(Collectors.toList());
    }

    /**
     * 根据逻辑表字段map-->逻辑表字段表达式Map
     * key:fieldCode, value:expression
     *
     * @Author anning
     * @Date 2024/7/4
     */
    public Map<String, String> getColumnExpMap() {
        if (MapUtils.isEmpty(factTableFieldMap)) {
            return null;
        }
        return factTableFieldMap.entrySet().stream()
                .collect(Collectors.toMap(
                        entry -> String.valueOf(entry.getKey()),
                        entry -> entry.getValue().getExpression()
                ));
    }
}
